# a
arrange(med_mass_flipper_ratio)
# b
group_by(species)
# c
penguins
# d
summarize(
med_mass_flipper_ratio = median(mass_flipper_ratio)
)
# e
mutate(
mass_flipper_ratio = body_mass_g / flipper_length_mm
)Data Wranging with dplyr
0.1 Learning Targets
In this lesson, you’ll learn
More about packages in R
More about the differences between
lists,data.frames, andtibblesHow to wrangle data using the dplyr package in the tidyverse
When you are finished with the readings and videos, you should be able to…
Describe to someone what a function conflict is
Describe to someone the similarities and differences between a
list, adata.frame, and atibbleIdentify the structure of an object
Identify the data type(s) of an object
Describe to someone what the tidyverse is
Use the five main dplyr verbs:
filter()arrange()select()mutate()summarize()
Use
group_by()to perform groupwise operationsUse the pipe operator (
|>or|>) to chain together data wrangling operations
0.2 Presentation Slides
\(\hspace{0.25in}\) 🔗 Link to All Presentation Slides
1 Part One: Learning More about Packages, Data Types, and Objects
▶️ Watch Videos: 30 minutes
📖 Readings: 10 minutes
✅ Preview Activities: 1
1.1 Learning More About Packages in R
▶️ Required Video: Why do I get this message when I load the tidyverse? – 8 minutes
To remove the package loading / data loading messages, you have two options:
- Globally turn off all messages by specifying
message: falseas anexecuteoption in your YAML - Locally turn off messages for a specific code chunk by specifying
#| message: falseas a code chunk option
1.2 Learning More about Data Types & Objects in R
▶️ Required Video: Learning More about Data Types – 11 minutes
▶️ Required Video: A Bit More on Object Structures – 8 minutes
1.3 ✅ Preview Activity 6: Data Structures
Question 1:
This lecture did not explicitly discuss the important structure of data.frames.
That’s because, in essence, a data.frame is simply a special list - with a few extra restrictions on the list format.
Think about the datasets you have already worked with. Which of the following restrictions on a list do you think are needed for the list to be a data.frame? (Select all that apply)
- The elements of the list must all be vectors of the same length.
- The elements of the list must all be the same data type.
- The elements of the list must all have no missing values.
- The elements of the list must all have names.
Question 2:
Tibbles are described as “opinionated” dataframes. Which of the following are true about a tibble’s behavior? (Select all that apply)
tibbles only print the first 10 rows of a datasettibbles allow for non-syntactic variable names, like:)tibbles never convert strings to factorstibbles create row names
2 Part Two: Wrangling data with dplyr
▶️ Watch Videos: 60 minutes
📖 Readings: 0-60 minutes
💻 Activities: 20-30 minutes
✅ Preview Activities: 1
2.1 Introduction to dplyr
▶️ Required Video: Intro to the tidyverse & dplyr – 8 minutes
📖 Required Reading: Data Transformation
❓ Additional Resource: Modern Dive Textbook – Section 3.1 through 3.6
2.2 dplyr Verbs
2.3 The Pipe Operator
2.4 Practice
💻 Required Tutorial: Practice with dplyr
✅ Preview Activity 7: Data Wrangling
Question 1: Suppose we would like to study how the ratio of penguin body mass to flipper size differs across the species. Arrange the following steps into an order that accomplishes this goal (assuming the steps are connected with a |> or a |>).
You can check your answers using the penguins data from the palmerpenguins R package!
Question 2:
Consider the base R code below.
mean(penguins[penguins$species == "Adelie", ]$body_mass_g)For each of the following dplyr pipelines, indicate which of the following is true:
- It returns the exact same thing as the (above) base R code
- It returns the correct information, but the wrong object type
- It returns incorrect information
- It returns an error
# Part a
penguins |>
filter("body_mass_g") |>
pull("Adelie") |>
mean()
# Part b
penguins |>
filter(species == "Adelie") |>
select(body_mass_g) |>
summarize(mean(body_mass_g))
# Part c
penguins |>
pull(body_mass_g) |>
filter(species == "Adelie") |>
mean()
# Part d
penguins |>
filter(species == "Adelie") |>
select(body_mass_g) |>
mean()
# Part e
penguins |>
filter(species == "Adelie") |>
pull(body_mass_g) |>
mean()
# Part f
penguins |>
select(species == "Adelie") |>
filter(body_mass_g) |>
summarize(mean(body_mass_g))You can check your answers using the penguins data from the palmerpenguins R package!